<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page import="entity.Admin" %>
        <%@ page import="entity.WarningThreshold" %>
            <%@ page import="entity.FeeType" %>
                <%@ page import="dao.WarningThresholdDao" %>
                    <%@ page import="dao.FeeTypeDao" %>
                        <%@ page import="java.util.List" %>
                            <html>

                            <head>
                                <title>预警阈值设置</title>
                                <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
                                    rel="stylesheet">
                                <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css"
                                    rel="stylesheet">
                                <style>
                                    body {
                                        background-color: #f8f9fa;
                                        padding-top: 2rem;
                                    }

                                    .card {
                                        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                                        border: none;
                                    }
                                </style>
                            </head>

                            <body>
                                <% Admin admin=(Admin) session.getAttribute("admin"); if (admin==null) {
                                    response.sendRedirect("../adminLogin.jsp"); return; } WarningThresholdDao
                                    thresholdDao=new WarningThresholdDao(); FeeTypeDao feeTypeDao=new FeeTypeDao();
                                    List<WarningThreshold> thresholds = thresholdDao.getAllThresholds();
                                    List<FeeType> feeTypes = feeTypeDao.getAllFeeTypes();
                                        %>

                                        <div class="container">
                                            <div class="row justify-content-center">
                                                <div class="col-md-10">
                                                    <div class="d-flex justify-content-between align-items-center mb-4">
                                                        <div class="d-flex align-items-center">
                                                            <a href="dashboard.jsp"
                                                                class="btn btn-outline-secondary me-3">
                                                                <i class="bi bi-arrow-left"></i> 返回控制台
                                                            </a>
                                                            <h2>预警阈值设置</h2>
                                                        </div>
                                                        <button type="button" class="btn btn-primary"
                                                            data-bs-toggle="modal" data-bs-target="#addThresholdModal">
                                                            <i class="bi bi-plus-lg"></i> 添加预警阈值
                                                        </button>
                                                    </div>

                                                    <div class="card">
                                                        <div class="card-body">
                                                            <div class="table-responsive">
                                                                <table class="table table-hover">
                                                                    <thead class="table-light">
                                                                        <tr>
                                                                            <th>费用类型</th>
                                                                            <th>预警阈值</th>
                                                                            <th>状态</th>
                                                                            <th>操作</th>
                                                                        </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                        <% for (WarningThreshold threshold : thresholds)
                                                                            { %>
                                                                            <tr>
                                                                                <td>
                                                                                    <%= threshold.getFeeName() %>
                                                                                </td>
                                                                                <td>¥<%= threshold.getThreshold() %>
                                                                                </td>
                                                                                <td>
                                                                                   <span class="badge <%= (threshold.getStatus() != null && threshold.getStatus() == 1) ? "bg-success" : "bg-danger" %>">
    <%= (threshold.getStatus() != null && threshold.getStatus() == 1) ? "禁用" : "启用" %>
</span>

                                                                                </td>
                                                                                <td>
                                                                                    <button
                                                                                        class="btn btn-sm btn-outline-primary"
                                                                                        onclick="editThreshold(<%= threshold.getId() %>)">
                                                                                        <i class="bi bi-pencil"></i>
                                                                                    </button>
                                                                                    <button
                                                                                        class="btn btn-sm btn-outline-danger ms-1"
                                                                                        onclick="deleteThreshold(<%= threshold.getId() %>)">
                                                                                        <i class="bi bi-trash"></i>
                                                                                    </button>
                                                                                </td>
                                                                            </tr>
                                                                            <% } %>
                                                                    </tbody>
                                                                </table>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                        <!-- 添加预警阈值模态框 -->
                                        <div class="modal fade" id="addThresholdModal" tabindex="-1">
                                            <div class="modal-dialog">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <h5 class="modal-title">添加预警阈值</h5>
                                                        <button type="button" class="btn-close"
                                                            data-bs-dismiss="modal"></button>
                                                    </div>
                                                    <form
                                                        action="${pageContext.request.contextPath}/admin/warningThreshold/add"
                                                        method="post">
                                                        <div class="modal-body">
                                                            <div class="mb-3">
                                                                <label class="form-label">费用类型</label>
                                                                <select class="form-select" name="feeTypeId" required>
                                                                    <% for (FeeType feeType : feeTypes) { %>
                                                                        <option value="<%= feeType.getId() %>">
                                                                            <%= feeType.getName() %>
                                                                        </option>
                                                                        <% } %>
                                                                </select>
                                                            </div>
                                                            <div class="mb-3">
                                                                <label class="form-label">预警阈值</label>
                                                                <input type="number" class="form-control"
                                                                    name="threshold" step="0.01" min="0" required>
                                                            </div>
                                                            <div class="mb-3">
                                                                <label class="form-label">状态</label>
                                                                <select class="form-select" name="status">
                                                                    <option value="1">启用</option>
                                                                    <option value="0">禁用</option>
                                                                </select>
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary"
                                                                data-bs-dismiss="modal">取消</button>
                                                            <button type="submit" class="btn btn-primary">保存</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>

                                        <script
                                            src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
                                        <script>
                                            function editThreshold(id) {
                                                window.location.href = '${pageContext.request.contextPath}/admin/warningThreshold/edit?id=' + id;
                                            }

                                            function deleteThreshold(id) {
                                                if (confirm('确定要删除这个预警阈值吗？')) {
                                                    window.location.href = '${pageContext.request.contextPath}/admin/warningThreshold/delete?id=' + id;
                                                }
                                            }
                                        </script>
                            </body>

                            </html>